package de.nak.fireDepartment.visitorAction;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.web.struts.LookupDispatchActionSupport;

import de.nak.fireDepartment.actionForms.EquipmentForm;
import de.nak.fireDepartment.model.Equipment;
import de.nak.fireDepartment.service.EquipmentService;

/**
 * Action that shows a equipment
 * 
 * @author Nils Bredow
 */
public class EquipmentAction extends LookupDispatchActionSupport {

	/**
	 * This method shows the selected equipment into the equipmentForm
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return actionforward
	 * @throws Exception
	 */	
	public ActionForward showEquipment(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		// ActionForm => EquipmentForm
		EquipmentForm equipmentForm = (EquipmentForm) form;
		EquipmentService equipmentService = (EquipmentService) getWebApplicationContext().getBean("equipmentService");
		
		// if no line selected display error
		if(equipmentForm.getSelectedKey()==null){
			equipmentForm.setErrorCode("text.error.selectedKey");
			return mapping.findForward("cancel");
		}
		
		// get equipment
		Equipment equipment = equipmentService.showEquipment(equipmentForm.getSelectedKey());
		
		// if equipment has no image set default image
		if (equipment.getImageURL() == null || equipment.getImageURL().equals("")){
			equipment.setImageURL("/FireDepartmentManagement/images/layout/noImage.jpg");
		}
		
		// store equipment in Form
		equipmentForm.setEquipment(equipment);
		// Return result
		return mapping.findForward("show");
	}
	
	/** {@inheritDoc} */
	@Override
	protected Map<String, String> getKeyMethodMap() {
		Map<String, String> map = new HashMap<String, String>();
		map.put("button.show", "showEquipment");
		return map;
	}
}
